from lxml import etree
import json

class Parser:
    def __init__(self, host, response, url):
        self.host = host
        self.response = response
        self.url = url
        self.hub_host = {
            'search.sina.com.cn': self.sina_parse,
            'so.news.cn': self.xhnews_parse,
            'www.toutiao.com': self.toutiao_parse,
            'nine.ifeng.com': self.fh_parse
        }

    def sina_parse(self):
        content = etree.HTML(self.response)
        href = content.xpath('//div[@class="box-result clearfix"]//h2/a/@href')
        return href

    def xhnews_parse(self):
        content = json.loads(self.response).get('content').get('results')
        href = [res.get('url') for res in content]
        return href

    def toutiao_parse(self):
        href = []
        datas = json.loads(self.response).get('data')
        for data in datas:
            url = data.get('article_url')
            if url and not url.endswith('mp4'):
                href.append(url)
        return href

    def fh_parse(self):
        href = []
        datas = json.loads(self.response)[0].get('item')
        for data in datas:
            link = data.get('link')
            if link.get('type') == 'doc':
                url = link.get('url')
                href.append(url)
        return href

    def execute(self):
        parser = self.hub_host.get(self.host, False)
        if parser:
            return parser()
        else:
            return False
